package com.hqd.ch03.v13.beans.property.support;

import com.hqd.ch03.v13.beans.property.PropertyEditorRegistry;
import com.hqd.ch03.v13.beans.propertyeditors.CustomBooleanEditor;
import com.hqd.ch03.v13.beans.propertyeditors.CustomNumberEditor;

import java.beans.PropertyEditor;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 类型转化器注册类
 */
public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
    /**
     * 用户注册编辑器
     */
    private Map<Class<?>, PropertyEditor> customEditors = new LinkedHashMap<>();
    /**
     * 默认编辑器
     */
    private Map<Class<?>, PropertyEditor> defaultEditors = new LinkedHashMap<>();

    public PropertyEditorRegistrySupport() {
        this.createDefaultEditors();
    }

    public PropertyEditor getDefaultEditor(Class<?> requiredType) {
        return this.defaultEditors.get(requiredType);
    }

    public PropertyEditor findCustomEditor(Class<?> requiredType) {
        return this.customEditors.get(requiredType);
    }

    private void createDefaultEditors() {
        this.defaultEditors.put(boolean.class, new CustomBooleanEditor());
        this.defaultEditors.put(Boolean.class, new CustomBooleanEditor());
        this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class));
        this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class));
        this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class));
        this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class));
    }

    @Override
    public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {
        this.customEditors.put(requiredType, propertyEditor);
    }
}
